#include <interfacelib/window/Box.h>
#include <interfacelib/window/window.h>
#include <interfacelib/directx/synchronized_dx.h>
#include <interfacelib/directx/initd3d.h>

using namespace interfacelib::Window;

Box::Box() : m_color( D3DCOLOR_ARGB( 255, 255,255,255 ) ), m_BoxVertexBuffer( NULL )
{
}

Box::~Box()
{
}

void Box::createBox()
{
	BOX_VERTEX Box[] = 
	{
		//{x,y,z,rhw,color},
		{ (float)0.0f, (float)0.0f, 0.0f,1,m_color}, // left top
		{ (float)0.0f, (float)0.0f, 0.0f,1,m_color}, // right top
		{ (float)0.0f, (float)0.0f, 0.0f,1,m_color}, // left bottom	
		{ (float)0.0f, (float)0.0f, 0.0f,1,m_color}  // right bottom
	};
//	interfacelib::DirectX::InitD3D::getInstance()->getDevice()->CreateVertexBuffer( sizeof(Box), 0, D3DFVF_BOX, D3DPOOL_MANAGED, &m_BoxVertexBuffer, NULL );
	m_BoxVertexBuffer = interfacelib::DirectX::VertexBufferManager::getInstance()->createVertexBuffer( sizeof(Box), 0, D3DFVF_BOX, D3DPOOL_MANAGED, NULL );

	
	{
		interfacelib::DirectX::Synchronized_DX_VertexBuffer sync_vertex( m_BoxVertexBuffer->vertexBuffer , sizeof( Box ) );

		void *vertices = sync_vertex.getVertices();
		memcpy(vertices, Box, sizeof( Box ) );
	}
}

BYTE Box::draw( RECT *rect )
{
	if ( !m_BoxVertexBuffer ) createBox();

	interfacelib::DirectX::Synchronized_Sprite_Rebegin sync;

	BOX_VERTEX BoxBuffer[] = 
	{
		//{x,y,z,rhw,color},
		{ (float)rect->left	, (float)rect->top, 0.0f,1,m_color}, // left top
		{ (float)rect->right, (float)rect->top, 0.0f,1,m_color}, // right top
		{ (float)rect->left	, (float)rect->bottom, 0.0f,1,m_color}, // left bottom	
		{ (float)rect->right, (float)rect->bottom, 0.0f,1,m_color}  // right bottom

	};

	interfacelib::DirectX::VertexBufferManager::getInstance()->createVertexBuffer( m_BoxVertexBuffer );
	{
		interfacelib::DirectX::Synchronized_DX_VertexBuffer sync_vertex( m_BoxVertexBuffer->vertexBuffer , sizeof( BoxBuffer ) );

		void *vertices = sync_vertex.getVertices();
		memcpy(vertices, BoxBuffer, sizeof( BoxBuffer ) );
	}

	interfacelib::DirectX::InitD3D::getInstance()->getDevice()->SetTexture( 0, NULL );
	interfacelib::DirectX::InitD3D::getInstance()->getDevice()->SetFVF( D3DFVF_BOX );
	interfacelib::DirectX::InitD3D::getInstance()->getDevice()->SetStreamSource( 0, m_BoxVertexBuffer->vertexBuffer, 0 , sizeof( BOX_VERTEX ) );
	interfacelib::DirectX::InitD3D::getInstance()->getDevice()->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

//	interfacelib::DirectX::VertexBufferManager::getInstance()->releaseVertexBuffer( m_BoxVertexBuffer );

	return ( 0 );
}